package com.junjie.checkin.config;

import com.github.pagehelper.PageInterceptor;
import com.junjie.checkin.entity.Enum.conventer.ActivityStatusEnumConverter;
import com.junjie.checkin.handller.mybatis.MybatisPlusHandler;
import com.junjie.checkin.properties.AliOssProperties;
import com.junjie.checkin.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author 洪浚杰
 * @description
 * @date 2025/6/9 14:55
 */
@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private RedisConnectionFactory factory;

    @Autowired
    private ActivityStatusEnumConverter activityStatusEnumConverter;

    /**
     * redis序列化器
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<String,Object>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }

    /**
     * 阿里云文件上传工具类
     * @param aliOssProperties
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public AliOssUtil aliOssUtil(AliOssProperties aliOssProperties){
        log.info("开始创建阿里云文件上传工具类对象：{}",aliOssProperties);
        return new AliOssUtil(aliOssProperties.getEndpoint(),
                aliOssProperties.getAccessKeyId(),
                aliOssProperties.getAccessKeySecret(),
                aliOssProperties.getBucketName());
    }

    @Bean
    public MybatisPlusHandler mybatisPlusHandler(){
        return new MybatisPlusHandler();
    }


    @Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }

    @Override
    public  void addFormatters(FormatterRegistry registry) {
        registry.addConverter(activityStatusEnumConverter);
    }
}
